package cn.school.demo.ui.register

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.pojo.Result
import cn.school.demo.pojo.User
import cn.school.demo.repository.DataRepository
import cn.school.demo.repository.RegisterRepository
import cn.school.demo.util.StringUtil
import com.blankj.utilcode.util.LogUtils

class RegisterViewModel : ViewModel() {

    private lateinit var _result: LiveData<Result>
    val result: LiveData<Result> get() = _result

    private lateinit var _getCode: LiveData<String>
    val getCode: LiveData<String> get() = _getCode

    private val _user = MutableLiveData<User>() //游客
    val user: LiveData<User> get() = _user

    private val _rePwd = MutableLiveData<String>()
    val rePwd: LiveData<String> get() = _rePwd

    private val _code = MutableLiveData<String>()
    val code: LiveData<String> get() = _code

    private val _id = MutableLiveData<String>()
    val id: LiveData<String> get() = _id

    init {
        _id.value = ""
        _code.value = ""
        _rePwd.value = ""
        _user.value = User(
            0,
            "",
            "",
            "",
            "山东工商学院",
            "计算机科学与技术学院",
            null,
            null,
            null,
            0,
            "",
            "",
            null,
            null,
            null,
            null
        )
    }

    fun setId(newId: String) {
        if (newId.length <= 10) {
            _id.value = newId
            if (newId != "") {
                _user.value?.sfNumber = newId.toLong().toInt()
            } else {
                _user.value?.sfNumber = 0
            }
        }
    }

    fun setPwd(newPwd: String) {
        _user.value?.password = newPwd
    }

    fun setPhone(newPhone: String) {
        _user.value?.telephoneNumber = newPhone
    }

    fun setName(newName: String) {
        _user.value?.nickname = newName
    }

    fun setRePwd(newPwd: String) {
        _rePwd.value = newPwd
    }

    fun setCode(newCode: String) {
        _code.value = newCode
    }

    fun setIdentity(identity: String) {
        _user.value!!.identity = identity
    }

    fun setCollage(college: String) {
        _user.value!!.college = college
    }

    fun setMajor(major: String) {
        _user.value!!.major = major
    }

    fun setGrade(grade: String) {
        _user.value!!.grade = grade
    }

    fun setTeam(team: String) {
        if (team != "")
            _user.value!!.team = team.toInt()
        else
            _user.value!!.team = null
    }

    fun getVerificationCodeDetection(): String? = when {
        _user.value!!.telephoneNumber.isEmpty() -> "手机号不能为空！"
        !StringUtil.isMobilePhone(_user.value!!.telephoneNumber) -> "手机号不合法！"
        else -> null
    }

    fun getTeacherRegisterDetection(): String? = when {
        _user.value!!.sfNumber.toString().isEmpty() -> "工号不能为空！"
        _user.value!!.nickname.isEmpty() -> "姓名不能为空!"
        _user.value!!.password.isEmpty() -> "密码不能为空！"
        _rePwd.value!!.isEmpty() -> "请再次输入密码！"
        _user.value!!.telephoneNumber.isEmpty() -> "手机号不能为空！"
        _code.value!!.isEmpty() -> "验证码不能为空！"
        !StringUtil.isStudentID(_user.value!!.sfNumber.toString()) -> "工号输入不合法！"
        !StringUtil.isName(_user.value!!.nickname) -> "姓名格式不合法！"
        (!StringUtil.isPassword(_user.value!!.password)) ||
                (!StringUtil.isPassword(_rePwd.value!!)) -> "密码输入不合法!"
        _user.value!!.password != _rePwd.value!! -> "两次密码不一致！"
        !StringUtil.isMobilePhone(_user.value!!.telephoneNumber) -> "手机号不合法！"
        !StringUtil.isCode(_code.value!!) -> "验证码不合法！"
        else -> null
    }

    fun getStudentRegisterDetection(): String? = when {
        _user.value!!.sfNumber.toString().isEmpty() -> "学号不能为空！"
        _user.value!!.nickname.isEmpty() -> "姓名不能为空!"
        _user.value!!.password.isEmpty() -> "密码不能为空！"
        _rePwd.value!!.isEmpty() -> "请再次输入密码！"
        _user.value!!.telephoneNumber.isEmpty() -> "手机号不能为空！"
        _code.value!!.isEmpty() -> "验证码不能为空！"
        !StringUtil.isStudentID(_user.value!!.sfNumber.toString()) -> "学号输入不合法！"
        !StringUtil.isName(_user.value!!.nickname) -> "姓名格式不合法！"
        (!StringUtil.isPassword(_user.value!!.password)) ||
                (!StringUtil.isPassword(_rePwd.value!!)) -> "密码输入不合法!"
        _user.value!!.password != _rePwd.value!! -> "两次密码不一致！"
        !StringUtil.isMobilePhone(_user.value!!.telephoneNumber) -> "手机号不合法！"
        !StringUtil.isCode(_code.value!!) -> "验证码不合法！"
        else -> null
    }

    fun getResult(user: User) {
        _result = RegisterRepository().register(user)
    }

    fun getCodeText(phone: String) {
        _getCode = DataRepository().getCode(phone)
    }

    override fun onCleared() {
        super.onCleared()
        LogUtils.e("我销毁了！")
    }
}